﻿using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using mojoPortal.Web.RS.SBM.AppCode.Lib;

namespace mojoPortal.Web.RS.SBM.Controls
{
    public partial class ModalManageLocationControl : RSBaseUserControl
    {

        int _companyid = 0;
        int _locationid = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            
        }
        
        protected void btnReturn_Click(object sender, EventArgs e)
        {
            if (hfRedirectURL.Value.Length > 0)
            {
                Response.Redirect(hfRedirectURL.Value);
            }
            else
                mpeModalManageLocation.Hide();
        }
        public void SetControlEnabledState(bool bstate = false)
        {
            RSSBMCommonFunctions.SetControlsEnabledState(UpdatePanelManageLocation.ContentTemplateContainer, bstate);
        }
        protected void btnEditLocation_Click(object sender, EventArgs e)
        {
            RSUserIdentity userIdentity = GetRSUserIdentity();
            if (userIdentity.RSCanWrite)
            {
                btnEditLocation.Visible = false;
                btnSaveLocation.Visible = true;
                btnDeleteLocation.Visible = true;
                SetControlEnabledState(true);
            }
            else
            {
                lblMessage.Text = "You Do Not Have Permission To Perform This Action!";
                lblMessage.ForeColor = System.Drawing.Color.DarkRed;
                btnEditLocation.Visible = false;
                btnSaveLocation.Visible = false;
                btnDeleteLocation.Visible = false;
                SetControlEnabledState(false);
            }
        }
        protected void btnSaveLocation_Click(object sender, EventArgs e)
        {
            RSUserIdentity userIdentity = GetRSUserIdentity();
            if (userIdentity.RSCanWrite)
            {
                int CompanyId = userIdentity.RSCompanyId;
                int LocationId = txtLocationId.Text == "" ? 0 : Convert.ToInt32(txtLocationId.Text);

                string LocationName = txtLocationName.Text.Trim();
                string Street = txtStreet.Text.Trim();
                string City = txtCity.Text.Trim();
                string County = txtCounty.Text.Trim();
                string State = txtState.Text.Trim();
                string Zip = txtZip.Text.Trim();
                string Note = txtLocationNote.Text.Trim();

                SqlCommand sqlCmd = new SqlCommand();

                SQLHelper.AddParamToSQLCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@LocationName", SqlDbType.NVarChar, 25, ParameterDirection.Input, LocationName);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@Street", SqlDbType.NVarChar, 25, ParameterDirection.Input, Street);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@City", SqlDbType.NVarChar, 25, ParameterDirection.Input, City);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@County", SqlDbType.NVarChar, 15, ParameterDirection.Input, County);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@State", SqlDbType.NVarChar, 15, ParameterDirection.Input, State);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@Zip", SqlDbType.NVarChar, 15, ParameterDirection.Input, Zip);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@Note", SqlDbType.NVarChar, -1, ParameterDirection.Input, Note);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@Active", SqlDbType.Bit, 0, ParameterDirection.Input, null);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@LocationId", SqlDbType.Int, 0, ParameterDirection.Input, LocationId);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@CompanyId", SqlDbType.Int, 0, ParameterDirection.Input, CompanyId);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@CurrentUser", SqlDbType.NVarChar, 25, ParameterDirection.Input, userIdentity.RSCurrentUser);
                SQLHelper.SetCommandType(sqlCmd, CommandType.StoredProcedure, "rs_uspSaveLocation");
                SQLHelper.ExecuteScalarCmd(sqlCmd);

                int returnValue = Convert.ToInt32(sqlCmd.Parameters["@ReturnValue"].Value);
                txtLocationId.Text = returnValue.ToString();
                lblMessage.Text = "Location Successfully Saved!";
                lblMessage.ForeColor = System.Drawing.Color.DarkBlue;
                hfRedirectURL.Value = Request.RawUrl;
            }
            else
            {
                lblMessage.Text = "You Do Not Have Permission To Perform This Action!";
                lblMessage.ForeColor = System.Drawing.Color.DarkRed;
            }
            btnEditLocation.Visible = false;
            btnSaveLocation.Visible = false;
            btnDeleteLocation.Visible = false;
            SetControlEnabledState(false);
        }

        protected void btnDeleteLocation_Click(object sender, EventArgs e)
        {
            //TODO: need cascading delete
            RSUserIdentity userIdentity = GetRSUserIdentity();
            if (userIdentity.RSCanWrite && userIdentity.RSCanDelete)
            {
                _companyid = userIdentity.RSCompanyId;
                if (!String.IsNullOrEmpty(Request.QueryString["locationid"]))
                    _locationid = Convert.ToInt32(Request.QueryString["locationid"].ToString());

                if (_companyid > 0 && _locationid > 0)
                {
                    SqlCommand sqlCmd = new SqlCommand();
                    SQLHelper.AddParamToSQLCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
                    SQLHelper.AddParamToSQLCmd(sqlCmd, "@locationid", SqlDbType.Int, 0, ParameterDirection.Input, _locationid);
                    SQLHelper.AddParamToSQLCmd(sqlCmd, "@companyid", SqlDbType.Int, 0, ParameterDirection.Input, _companyid);
                    SQLHelper.SetCommandType(sqlCmd, CommandType.Text, "DELETE FROM rs_tblLocation WHERE LocationId=@locationid AND CompanyId=@companyid");
                    SQLHelper.ExecuteScalarCmd(sqlCmd);
                    int returnValue = Convert.ToInt32(sqlCmd.Parameters["@ReturnValue"].Value);
                    hfRedirectURL.Value = Request.RawUrl;
                    lblMessage.Text = "Location deleted successfully!";
                    lblMessage.ForeColor = System.Drawing.Color.DarkBlue;
                }
            }
            else
            {
                lblMessage.Text = "You Do Not Have Permission To Perform This Action!";
                lblMessage.ForeColor = System.Drawing.Color.DarkRed;
            }
            btnEditLocation.Visible = false;
            btnSaveLocation.Visible = false;
            btnDeleteLocation.Visible = false;
            SetControlEnabledState(false);
        }

        public void ShowModal(int companyid, int locationid)
        {
            hfRedirectURL.Value = "";
            if(LoadLocationDataByID(companyid, locationid))
            {
                lblMessage.Text = "Location";
                btnEditLocation.Visible = true;
                btnSaveLocation.Visible = false;
                btnDeleteLocation.Visible = false;
                SetControlEnabledState(false);
            }
            else
            {
                lblMessage.Text = "Add New Service Location";
                btnEditLocation.Visible = false;
                btnSaveLocation.Visible = true;
                btnDeleteLocation.Visible = false;
                SetControlEnabledState(true);
            }
            mpeModalManageLocation.Show();
        }
        private bool LoadLocationDataByID(int companyid, int locationid)
        {
            bool bHasData = false;
            if (companyid > 0 && locationid > 0)
            {
                txtLocationId.Text = locationid.ToString();
                String strSQL = "select LocationId,LocationName,Street,City,County,State,Country,Zip,Active,Note,CompanyId from rs_tblLocation where locationid=@locationid and companyid=@companyid";
                SqlCommand myCommand;
                SqlParameter param;
                SqlConnection myConnection;
                myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings[GetRSCompanyConnectionString()].ToString());
                myCommand = new SqlCommand(strSQL, myConnection);
                SqlDataReader myDataReader;

                param = new SqlParameter();
                param.SqlDbType = SqlDbType.Int;
                param.ParameterName = "@locationid";
                param.Value = locationid;
                myCommand.Parameters.Add(param);

                param = new SqlParameter();
                param.SqlDbType = SqlDbType.Int;
                param.ParameterName = "@companyid";
                param.Value = companyid;
                myCommand.Parameters.Add(param);

                myConnection.Open();
                myDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
                if (myDataReader.HasRows)
                {
                    myDataReader.Read();
                    txtLocationId.Text = myDataReader["LocationId"].ToString();
                    txtLocationName.Text = myDataReader["LocationName"].ToString();
                    txtStreet.Text = myDataReader["Street"].ToString();
                    txtCity.Text = myDataReader["City"].ToString();
                    txtState.Text = myDataReader["State"].ToString();
                    txtCounty.Text = myDataReader["County"].ToString();
                    txtZip.Text = myDataReader["Zip"].ToString();
                    txtLocationNote.Text = myDataReader["Note"].ToString();

                    bHasData = true;
                }
                myConnection.Close();
            }
            return bHasData;
        }
    }
}